package com.zyf.ppx.utils

import com.orhanobut.logger.Logger
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * 作者：Think
 * 创建于 2019年2月13日 13:48:45
 */
object ServiceUtils {
    private const val API_BASE_URL = "http://lf-c.snssdk.com/"
    private var loggingInterceptor = HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message -> Logger.e("retrofitBack = $message") }).setLevel(HttpLoggingInterceptor.Level.BODY)
    private val httpClient = OkHttpClient.Builder()
        .connectTimeout(60, TimeUnit.SECONDS)
        .readTimeout(60, TimeUnit.SECONDS)
        .writeTimeout(60, TimeUnit.SECONDS)
        .addInterceptor(loggingInterceptor)
    /* var httpCacheDirectory = File(Environment.getExternalStorageDirectory(), "Connotation")//这里为了方便直接把文件放在了SD卡根目录的HttpCache中，一般放在context.getCacheDir()中
     var cacheSize = 10 * 1024 * 1024L//设置缓存文件大小为10M
     var cache = Cache(httpCacheDirectory, cacheSize)  .cache(cache)*/
    private val builder = Retrofit.Builder()
        .baseUrl(API_BASE_URL)
        .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
        .addConverterFactory(GsonConverterFactory.create())
        .client(httpClient.build())
        .build()

    fun <S> createService(serviceClass: Class<S>): S {
        return builder.create(serviceClass)
    }
}
